package com.kpmg.questionnaire.controller;

import com.kpmg.questionnaire.model.AnswerConfirmEntity;
import com.kpmg.questionnaire.model.AnswerEntity;
import com.kpmg.questionnaire.model.QuestionEntity;
import com.kpmg.questionnaire.repository.AnswerConfirmRepository;
import com.kpmg.questionnaire.repository.AnswerRepository;
import com.kpmg.questionnaire.repository.QuestionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by xionghao on 2016/11/9.
 */
@Controller
public class AnswerConfirmController {
    // 自动装配数据库接口，不需要再写原始的Connection来操作数据库

    @Autowired
    private AnswerConfirmRepository answerConfirmRepository;
    private QuestionRepository questionRepository;

//    答案查看
    @RequestMapping(value = "answerConfirm", method = RequestMethod.GET)
    public  String showAnswer(ModelMap modelMap){
//        List<QuestionEntity> questionList = questionRepository.findAll();
//        List<QuestionEntity> listq = questionRepository.findAll();
//        List<AnswerConfirmEntity> list = answerConfirmRepository.findId();
//        modelMap.addAttribute("list", list);
//        modelMap.addAttribute("listq", listq);
        return "answerConfirm/confirm";
    }

    // 答案添加
    @RequestMapping(value = "/answerConfirm/addItem", method = RequestMethod.GET)
    public String addItem(){
        return "answerConfirm/addItem";
    }

    // 添加处理
    @ResponseBody
    @RequestMapping(value = "/answerConfirm/addItemPost", method = RequestMethod.POST)
    public String addItemPost(@ModelAttribute("answerConfirm") AnswerConfirmEntity answerConfirmEntity){
        // 向数据库添加一个用户
        //userRepository.save(userEntity);

        // 向数据库添加一个表单，并将内存中缓存区的数据刷新，立即写入数据库，之后才可以进行访问读取
        answerConfirmRepository.saveAndFlush(answerConfirmEntity);

        // 返回重定向页面
        return "true";
    }

}
